package cn.doyens.reggie.controller;

import cn.doyens.reggie.common.R;
import cn.doyens.reggie.dto.DishDto;
import cn.doyens.reggie.entity.Category;
import cn.doyens.reggie.entity.Dish;
import cn.doyens.reggie.entity.DishFlavor;
import cn.doyens.reggie.service.CategoryService;
import cn.doyens.reggie.service.DishFlavorService;
import cn.doyens.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     *  添加菜品功能方法
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> saveDishWithFlavors(@RequestBody DishDto dishDto){

        this.dishService.saveDishWithFlavors(dishDto);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.success("添加菜品成功");
    }
   /* @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,String name){
        //1.创建Page对象
        Page<Dish> dishPage = new Page<>(page,pageSize);
        //2.创建查询构造器对象
        LambdaQueryWrapper<Dish> dishQueryWrapper  = new LambdaQueryWrapper<>();
        dishQueryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        //3.通过service调用page方法 执行分页查询
        this.dishService.page(dishPage,dishQueryWrapper);

        return R.success(dishPage);
    }*/
    /**
     *   查询菜品分页数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
   /* @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,String name){
        //1.创建Page对象
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //2.创建查询构造器对象
        LambdaQueryWrapper<Dish> dishQueryWrapper  = new LambdaQueryWrapper<>();
        dishQueryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        //3.通过service调用page方法 执行分页查询
        this.dishService.page(dishPage,dishQueryWrapper);
        //4.通过BeanUtils工具类调用 copyProperties方法 实现对象属性值的快速复制
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        log.info("dishPage对象的信息:"+dishPage);
        log.info("dishDtoPage对象的信息:"+dishDtoPage);
        //5.获取dishPage对象中的 records集合
        List<Dish> dishList = dishPage.getRecords();
        //6.遍历dishList
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            //取出每一个菜品对象中的categoryId
            Long categoryId = dish.getCategoryId();
            //通过categoryService调用方法获取指定id的分类对象
            Category catetory = this.categoryService.getById(categoryId);
            //判断catetory是否为空
            if(catetory!=null){
                //创建一个DishDto对象
                DishDto dishDto = new DishDto();
                //将category对象中的 name字段取出 赋值给DishDto对象
                dishDto.setCategoryName(catetory.getName());
                //通过BeanUtils工具类 将dish对象中的字段信息复制到dishDto对象中
                BeanUtils.copyProperties(dish,dishDto);
                dishDtoList.add(dishDto);
            }
        }
        //将dishDtoList 赋值到 dishDtoPage对象中的 records字段
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }
*/
    @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,String name){
        //1.创建Page对象
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //2.创建查询构造器对象
        LambdaQueryWrapper<Dish> dishQueryWrapper  = new LambdaQueryWrapper<>();
        dishQueryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        //3.通过service调用page方法 执行分页查询
        this.dishService.page(dishPage,dishQueryWrapper);
        //4.通过BeanUtils工具类调用 copyProperties方法 实现对象属性值的快速复制
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //5.获取dishPage对象中的 records集合
        List<Dish> dishList = dishPage.getRecords();
        //6.遍历dishList  (参数列表)->{方法体  return 返回值;}
        List<DishDto> dishDtoList = dishList.stream().map((dish) -> {
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            DishDto dishDto = new DishDto();
            if (category != null) {
                dishDto.setCategoryName(category.getName());
                BeanUtils.copyProperties(dish, dishDto);
            }
            return dishDto;
        }).collect(Collectors.toList());

        //将dishDtoList 赋值到 dishDtoPage对象中的 records字段
        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);
    }
    /**
     *  根据菜品id 查询 菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable Long id){

       DishDto dishDto =  this.dishService.queryDishWithFlavors(id);

       return R.success(dishDto);
    }
    /**
     * 修改菜品功能
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        this.dishService.updateDish(dishDto);

        return R.success("修改菜品成功");
    }


    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
          String key="dish_"+categoryId;
        ValueOperations ops = redisTemplate.opsForValue();
//        List<DishDto> dishDtoList=null;
       List<DishDto> dishDtoList = (List<DishDto>) ops.get(key);
         if(dishDtoList!=null){
                return R.success(dishDtoList);
         }
        //创建条件构造器对
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        if(categoryId!=null){
            queryWrapper.eq("category_id",categoryId);
            queryWrapper.orderByDesc("update_time");
        }
        //通过dishService调用方法查询 符合条件的 菜品信息
        List<Dish> list = this.dishService.list(queryWrapper);

       dishDtoList = list.stream().map(dish -> {
            //创建DishDto对象
            DishDto dishDto = new DishDto();
            //通过BeanUtil工具类 将 dish对象中的字段 赋值给dishDto
            BeanUtils.copyProperties(dish, dishDto);
            //通过dish的id 查询出 dish_flavor表中的 相关的 口味对象集合
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
            //通过dishFlavorService 调用方法查询 指定菜品相关的口味对象集合
            List<DishFlavor> dishFlavorList = this.dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            //将查询到的口味对象集合 赋值给 dishDto
            dishDto.setFlavors(dishFlavorList);
            //返回赋值后的dishDto对象
            return dishDto;
        }).collect(Collectors.toList());
             ops.set(key,dishDtoList);
        return R.success(dishDtoList);
    }

}
